﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OnlineManagement.Common
{
   public static class IFExtend
    {
       /// <summary>
       /// 只能用于应用类型
        /// 参考http://tech.ddvip.com/2009-08/1251293953128033.html
       /// </summary>
       /// <typeparam name="T"></typeparam>
       /// <param name="t"></param>
       /// <param name="predicate"></param>
       /// <param name="action"></param>
       /// <returns></returns>
       public static T If<T>(this T t, Predicate<T> predicate, Action<T> action) where T : class
       {
           if (t == null) throw new ArgumentNullException();
           if (predicate(t)) action(t);
           return t;
       }
       /// <summary>
       /// 值类型的if 链式表达式
       /// </summary>
       /// <typeparam name="T"></typeparam>
       /// <param name="t"></param>
       /// <param name="predicate"></param>
       /// <param name="func"></param>
       /// <returns></returns>
       public static T If<T>(this T t, Predicate<T> predicate, Func<T, T> func) where T : struct
       {
           return predicate(t) ? func(t) : t;
       }

       /// <summary>
       /// String 类型的链式表达式
       /// </summary>
       /// <param name="s"></param>
       /// <param name="predicate"></param>
       /// <param name="func"></param>
       /// <returns></returns>
       public static string If(this string s,Predicate<string> predicate,Func<string,string> func) {
           return predicate(s) ? func(s) : s;
       }
     
    }
}
